package com.longrise.co.ui.carlife.presenter;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carlife.contract.CarLifeContract;
import com.longrise.co.ui.carlife.model.IllegalInquiriesBean;
import com.longrise.co.ui.carlife.model.NewsBean;
import com.longrise.co.ui.carlife.model.YearCheckBean;
import com.ly.common.baserx.RxSubscriber;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.security.EnCryptDeCrypt2;

/**
 * Created by luoyang on 2017-6-23.
 */

public class CarLifePresenter extends CarLifeContract.Presenter {

    @Override
    public void getLifeNewsList(int pageNum,int pageSize, boolean isShowProcess) {
        try {

            EntityBean bean = new EntityBean();

            bean.set("areaid", CommonPara.getInstance().getAreaId());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("pagesize", String.valueOf(pageSize));
            bean.set("pagenum", pageNum);
            bean.set("sign", CommonPara.getInstance().getEnAreaId());

            mRxManage.add(mModel.carLifeNewsRequest(bean).subscribe(new RxSubscriber<NewsBean>(mContext, isShowProcess) {
                @Override
                protected void _onNext(NewsBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getIllegalInquiries(String carNo, String vin, String engineNumber) {
        try {

            if(StringUtils.isEmpty(carNo) || StringUtils.isEmpty(vin)) {
                mView.showErrorTip("违章查询参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();

            String enCarNo = EnCryptDeCrypt2.getInstance().EnCrypt(carNo);
            bean.set("carno", carNo);
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("carvin", vin);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("cartype", "02");
            bean.set("userid", CommonPara.getInstance().getmUserId());
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("enginecode",engineNumber);
            bean.set("sign", enCarNo);

            mRxManage.add(mModel.illegalInquiriesRequest(bean).subscribe(new RxSubscriber<IllegalInquiriesBean>(mContext, true) {
                @Override
                protected void _onNext(IllegalInquiriesBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getYearCheckRequest(String carNo, String time, String carTypeCode) {
        try{
            if(StringUtils.isEmpty(carNo) || StringUtils.isEmpty(time) || StringUtils.isEmpty(carTypeCode)) {
                mView.showErrorTip("违章查询参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();

            String enCarNo = EnCryptDeCrypt2.getInstance().EnCrypt(carNo);
            bean.set("carno", carNo);
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("cartype", carTypeCode);
            bean.set("time", time);
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enCarNo);

            mRxManage.add(mModel.yearCheckRequest(bean).subscribe(new RxSubscriber<YearCheckBean>(mContext, true) {
                @Override
                protected void _onNext(YearCheckBean bean) {
                    mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void getKckpUrl() {
        try{
            EntityBean bean = new EntityBean();

            bean.set("uid", CommonPara.getInstance().getmUserId());
            bean.set("mobilephone", CommonPara.getInstance().getLoginBean().getMobile());
            bean.set("type", "26");

            mRxManage.add(mModel.kckpUrlRequest(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String bean) {
                   if(mView instanceof CarLifeContract.CarLifeView) {
                       ((CarLifeContract.CarLifeView) mView).getKcKpUrlSuccess(bean);
                   }
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }

    @Override
    public void kckpUploadPhoto(String imageData, String type) {
        try{
            if(StringUtils.isEmpty(imageData) || StringUtils.isEmpty(type)) {
                mView.showErrorTip("上传图片参数有误");
                return ;
            }
            EntityBean bean = new EntityBean();

            bean.set("pic", imageData);
            bean.set("type", type);

            mRxManage.add(mModel.kckpUploadPhotoRequest(bean).subscribe(new RxSubscriber<String>(mContext, true) {
                @Override
                protected void _onNext(String bean) {
                   mView.requestSuccess(bean);
                }

                @Override
                protected void _onError(String message) {
                    mView.showErrorTip(message);
                }

                @Override
                public void onCompleted() {
                    super.onCompleted();
                    mView.onComplete();
                }
            }));

        } catch (Exception e) {
            e.printStackTrace();
            mView.showErrorTip("请求失败");
        }
    }
}
